package com.js.rtu.fragment;


import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.js.rtu.R;
import com.js.rtu.config.ErrConfig;
import com.js.rtu.custom.BottomBar;
import com.js.rtu.custom.BottomBarTab;
import com.js.rtu.event.StartBrotherEvent;
import com.js.rtu.event.TabSelectedEvent;
import com.js.rtu.fragment.first.FirstTabFragment;
import com.js.rtu.fragment.forth.bd.BDBean;
import com.js.rtu.fragment.forth.ForthTabFragment;
import com.js.rtu.fragment.second.SecondTabFragment;
import com.js.rtu.fragment.third.ThirdTabFragment;
import com.js.rtu.util.BleUtils;
import com.js.rtu.util.CRC16Util;
import com.js.rtu.util.MyLog;
import com.js.rtu.util.RxTimerUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import me.yokeyword.eventbusactivityscope.EventBusActivityScope;
import me.yokeyword.fragmentation.SupportFragment;

/**
 * 主fragment
 * Created by wz on 169/3/21.
 */
public class MainFragment extends SupportFragment {

    public static final int FIRST = 0;
    public static final int SECOND = 1;
    public static final int THIRD = 2;
    public static final int FORTH = 3;

    @BindView(R.id.bottomBar)
    BottomBar bottomBar;
    Unbinder unbinder;

    private SupportFragment[] mFragments = new SupportFragment[4];


    public static MainFragment newInstance() {
        Bundle args = new Bundle();
        MainFragment fragment = new MainFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_main, container, false);
        EventBus.getDefault().register(this);
        unbinder = ButterKnife.bind(this, view);
        initView();
//        String str="EE EE 30 32 30 30 30 30 30 38 A9 00 90 0E 00 00 00 00 0F 00 0F F5 E0 40 10 3C 01 00 00 11 8D 01 00 00 12 29 00 00 00 13 28 00 00 00 14 00 00 00 00 15 53 01 00 00 16 4E 35 2E 32 2E 30 37 00 17 57 14 00 00 18 47 57 2D 38 30 42 34 2D 44 43 4E 00 00 00 00 00 19 00 00 00 00 20 00 00 00 00 21 00 00 00 00 22 00 00 00 00 23 00 00 00 00 24 00 00 00 00 25 09 00 00 00 26 16 03 01 0D 2B 13 27 A5 00 28 28 00 00 00 29 44 44 1B 04 03 97 30 E0 2E B2 3E B0 0E 03 00 31 9C C1 9F 87 68 A2 01 00 32 00 00 00 00 00 00 00 00 FD 34 FF FF";
//
//        for (String s : str.split(" ")) {
//            if (!s.isEmpty()){
//             s=   s.replace(" ","");
//                dataList.add(s);
//            }
//
//        }
//        startReveiveData();
        return view;
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        SupportFragment firstFragment = findChildFragment(FirstTabFragment.class);
        if (firstFragment == null) {
            mFragments[FIRST] = FirstTabFragment.newInstance();
            mFragments[SECOND] = SecondTabFragment.newInstance();
            mFragments[THIRD] = ThirdTabFragment.newInstance();
            mFragments[FORTH] = ForthTabFragment.newInstance();
            loadMultipleRootFragment(R.id.fl_container, FIRST,
                    mFragments[FIRST],
                    mFragments[SECOND],
                    mFragments[THIRD],
                    mFragments[FORTH]);
        } else {
            mFragments[FIRST] = firstFragment;
            mFragments[SECOND] = findChildFragment(SecondTabFragment.class);
            mFragments[THIRD] = findChildFragment(ThirdTabFragment.class);
            mFragments[FORTH] = findChildFragment(ForthTabFragment.class);

        }
    }

    private void initView() {
        bottomBar
                .addItem(new BottomBarTab(_mActivity, R.mipmap.ic_tab_first, getString(R.string.navigation_title_connect)))
                .addItem(new BottomBarTab(_mActivity, R.mipmap.ic_tab_second, getString(R.string.navigation_title_product)))
                .addItem(new BottomBarTab(_mActivity, R.mipmap.ic_tab_third, getString(R.string.navigation_title_group)))
                .addItem(new BottomBarTab(_mActivity, R.mipmap.ic_tab_forth, getString(R.string.navigation_title_data)));


        bottomBar.setOnTabSelectedListener(new BottomBar.OnTabSelectedListener() {
            @Override
            public void onTabSelected(int position, int prePosition) {
                showHideFragment(mFragments[position], mFragments[prePosition]);
            }

            @Override
            public void onTabUnselected(int position) {

            }

            @Override
            public void onTabReselected(int position) {
                // 在FirstPagerFragment,FirstHomeFragment中接收, 因为是嵌套的Fragment
                // 主要为了交互: 重选tab 如果列表不在顶部则移动到顶部,如果已经在顶部,则刷新
                EventBusActivityScope.getDefault(_mActivity).post(new TabSelectedEvent(position));
            }
        });
    }

    @Override
    public void onFragmentResult(int requestCode, int resultCode, Bundle data) {
        super.onFragmentResult(requestCode, resultCode, data);

    }

    /**
     * start other BrotherFragment
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void startFragment(StartBrotherEvent event) {
        start(event.targetFragment);
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
        unbinder.unbind();
    }




}

